package com.hwpt.hwaccount.feature.auth;

import android.util.Log;

import com.hwpt.hwaccount.core.data.remote.api.ApiService;
import com.hwpt.hwaccount.core.data.remote.request.ResetPasswordRequest;
import com.hwpt.hwaccount.core.data.remote.request.SendCodeRequest;
import com.hwpt.hwaccount.core.data.remote.request.VerifyCodeRequest;
import com.hwpt.hwaccount.core.utils.NetworkResponseHandler;
import com.hwpt.hwaccount.feature.auth.utils.AuthValidator;


public class RepwdModel implements RepwdContract.Model {

    private final ApiService apiService;

    public RepwdModel(ApiService apiService) {
        this.apiService = apiService;
    }

    @Override
    public void sendVerificationCode(String phone, RepwdContract.OnVerificationCodeSentListener listener) {
        // 验证手机号
        String error = AuthValidator.getAccountError(phone);
        if (error != null) {
            listener.onFailure(error);
            return;
        }

        SendCodeRequest request = new SendCodeRequest(phone, "ResetPassword");
        Log.d("RepwdModel", "发送验证码请求: " + phone + ", 场景: reset");
        NetworkResponseHandler.handleSimpleResponse(
                apiService.sendVerifyCode(request),
                new NetworkResponseHandler.SimpleResponseCallback() {
                    @Override
                    public void onSuccess() {
                        listener.onSuccess();
                    }

                    @Override
                    public void onFailure(String errorMsg) {
                        listener.onFailure(errorMsg);
                    }
                }
        );
    }

    @Override
    public void verifyCode(String phone, String code, RepwdContract.OnCodeVerifiedListener listener) {
        // 验证手机号和验证码
        String phoneError = AuthValidator.getAccountError(phone);
        if (phoneError != null) {
            listener.onFailure(phoneError);
            return;
        }

        String codeError = AuthValidator.getVerifyCodeError(code);
        if (codeError != null) {
            listener.onFailure(codeError);
            return;
        }

        VerifyCodeRequest request = new VerifyCodeRequest(phone, code);
        NetworkResponseHandler.handleSimpleResponse(
                apiService.verifyCode(request),
                new NetworkResponseHandler.SimpleResponseCallback() {
                    @Override
                    public void onSuccess() {
                        listener.onSuccess();
                    }

                    @Override
                    public void onFailure(String errorMsg) {
                        listener.onFailure(errorMsg);
                    }
                }
        );
    }

    @Override
    public void resetPassword(String phone, String newPassword, RepwdContract.OnPasswordResetListener listener) {
        // 验证密码
        String passwordError = AuthValidator.getPasswordError(newPassword);
        if (passwordError != null) {
            listener.onFailure(passwordError);
            return;
        }

        ResetPasswordRequest request = new ResetPasswordRequest(phone, newPassword);
        NetworkResponseHandler.handleSimpleResponse(
                apiService.resetPassword(request),
                new NetworkResponseHandler.SimpleResponseCallback() {
                    @Override
                    public void onSuccess() {
                        listener.onSuccess();
                    }

                    @Override
                    public void onFailure(String errorMsg) {
                        listener.onFailure(errorMsg);
                    }
                }
        );
    }
}